package com.yc.web.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Random;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.oyj.dao.DBHelper;
@WebServlet("/order")
public class orderServlet extends BasicServlet {
	private DBHelper db = new DBHelper();
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setCharacterEncoding("utf-8");
		resp.setContentType("text/plain;charset=utf-8");
		
		//得到op
		String op = req.getParameter("op");
		
		
		
		//开始判断
		if("findGoodsInfoByCnos".equals(op)){
			findGoodsInfoByCnos(req,resp);
		}else if("add".equals(op)){
			add(req,resp);
		}else if ("findAll".equals(op)){
			findAll(req,resp);
		}
	}

	//查询所有订单
	private void findAll(HttpServletRequest req, HttpServletResponse resp) {
		if( req.getSession().getAttribute("loginUser") == null){
			return;
		}
		Map<String,Object> user = (Map<String, Object>) req.getSession().getAttribute("loginUser");
		int mno = Integer.parseInt(user.get("mno") + "");
		
		String sql = "select o.ono, DATE_FORMAT(o.odate,'%Y-%m-%d %H:%i') as odate   ,o.price as totalPrice,o.status, oi.gno,oi.nums,oi.price,gname,pics,weight,unit  "
				+ "from orderinfo o , orderiteminfo oi, goodsinfo g , addrinfo a  where o.ono = oi.ono and "
				+ "oi.gno = g.gno  and a.ano = o.ano and a.mno = ? order by o.ono desc";
		this.send(resp, db.findAll(sql, mno));
		
	}

	private void add(HttpServletRequest req, HttpServletResponse resp) {
		String cnos = req.getParameter("cnos");
		String price = req.getParameter("price");
		String ano = req.getParameter("ano");
		System.out.println(cnos);
		//生成订单编号
		String ono = new Date().getTime() + "" + new Random().nextInt(100000);
		//添加订单
		String sql = "insert into orderinfo values(?,now(),?,null,null,1,?,0)";
		int result = db.doUpdate(sql, ono,ano,price);
		
		//添加订单详情表
		String sql2 = "insert into orderiteminfo select 0,?,cartinfo.gno,num,price,1 from cartinfo ,"
					+" goodsinfo where cartinfo.gno = goodsinfo.gno and cno in (";
		String[] arr = cnos.split(",");
		List<Object> params = new ArrayList<Object>();
		params.add(ono);
		for(String s : arr){
			sql2 +="?, ";
			params.add(s);
		}
		sql2 = sql2.substring(0,sql2.lastIndexOf(",")) + ")";
		int result2 = db.doUpdate(sql2, params);
		
		//删除购物车里面的数据
		String sql3 = "delete from cartinfo where cno in(";
		params = new ArrayList<Object>();
		for(String s : arr){
			sql3 +="?, ";
			params.add(s);
		}	
		sql3 = sql3.substring(0,sql3.lastIndexOf(",")) + ")";
		int result3 = db.doUpdate(sql3, params);
		
		if( result > 0 && result2 > 0 && result3 > 0){
			this.send(resp, ono);
		}else{
			this.send(resp, 0);
		}
		
	}

	private void findGoodsInfoByCnos(HttpServletRequest req, HttpServletResponse resp) {
		String cnos = req.getParameter("cnos");
		String[] arr = cnos.split(",");
		
		String sql = "select  * from cartinfo c, goodsinfo g where g.gno = c.gno and cno in (";
		
		List<Object> params = new ArrayList<Object>();
		for(String s : arr){
			sql +="?, ";
			params.add(s);
		}
		sql = sql.substring(0,sql.lastIndexOf(","));
		sql +=")";
		List<Map<String,Object>> list = db.findAll(sql, params);
		if( list.size() <= 0){
			this.send(resp, 0);
		}else{
			this.send(resp, list);
		}
	}

}
